package com.gky.gkytemplate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gky.gkytemplate.common.constant.UserConstant;
import com.gky.gkytemplate.common.context.BaseContext;
import com.gky.gkytemplate.common.enums.ErrorCode;
import com.gky.gkytemplate.common.exception.BusinessException;
import com.gky.gkytemplate.pojo.dto.user.UserLoginDto;
import com.gky.gkytemplate.pojo.dto.user.UserQueryRequest;
import com.gky.gkytemplate.pojo.entity.User;
import com.gky.gkytemplate.pojo.vo.UserLoginVo;
import com.gky.gkytemplate.service.UserService;
import com.gky.gkytemplate.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author gky
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2024-08-23 19:10:40
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Override
    public UserLoginVo login(UserLoginDto userLoginDto) {
        User user = this.userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserAccount, userLoginDto.getUserAccount())
                .eq(User::getUserPassword, userLoginDto.getUserPassword()));
        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtils.copyProperties(user, userLoginVo);
        return userLoginVo;
    }

    @Override
    public boolean userLogout(HttpServletRequest request) {
        //删除redis的token
        Boolean result = redisTemplate.delete(UserConstant.USER_LOGIN + BaseContext.getCurrentId());
        if (result == false) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        //threadLocal
        BaseContext.removeCurrentId();
        return true;
    }

    @Override
    public Page<User> listPage(UserQueryRequest userQueryRequest) {
        int current = userQueryRequest.getCurrent();
        int pageSize = userQueryRequest.getPageSize();
        Page<User> page = new Page<>(current, pageSize);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //用户名
        if (StringUtils.isNotBlank(userQueryRequest.getUserName())) {
            userLambdaQueryWrapper.eq(User::getUserName, userQueryRequest.getUserName());
        }
        //角色
        if (StringUtils.isNotBlank(userQueryRequest.getUserRole())) {
            userLambdaQueryWrapper.eq(User::getUserRole, userQueryRequest.getUserRole());
        }
        if (StringUtils.isNotBlank(userQueryRequest.getUserProfile())) {
            userLambdaQueryWrapper.eq(User::getUserProfile, userQueryRequest.getUserProfile());
        }
        Page<User> userPage = this.userMapper.selectPage(page, userLambdaQueryWrapper);
        log.info("用户分页结果:{}", userPage.toString());
        return userPage;
    }
}




